const actions = {

}

const mutations = {
  //更新收货地址
  updateAddress(state, address) {
    state.address = address
    //本地持久化存储
    this.commit('user/saveToStorge')
  },
  //将地址持久化存储到本地存储 —— 真实开发不应该存到这，应该存在数据库中
  saveToStorge(state, address) {
    uni.setStorageSync('address', JSON.stringify(state.address))
  },
  //更新用户信息
  updateUserInfo(state, userInfo) {
    state.userInfo = userInfo
    this.commit('user/saveUserInfoToStorge')
  },

  //将用户信息持久化存储到本地存储中
  saveUserInfoToStorge(state, userInfo) {
    uni.setStorageSync('userInfo', JSON.stringify(state.userInfo))
  },

  //将token存到vuex中
  updateToken(state, token) {
    state.token = token
    this.commit('user/saveTokenToStorage')
  },
  //将token进行本地存储
  saveTokenToStorage(state) {
    uni.setStorageSync('token', state.token)
  },

  //更新重定向的对象
  updateRedirectInfo(state, info) {
    state.redirectInfo = info
  },

}

const state = {
  //收货地址
  address: JSON.parse(uni.getStorageSync('address') || '{}'),
  token: uni.getStorageSync('token') || '',
  //用户的基本信息
  userInfo: JSON.parse(uni.getStorageSync('userInfo') || '{}'),
  //重定向的object对象 {openType,from}
  redirectInfo: null
}

const getters = {
  //收货地址
  addStr(state) {
    if (!state.address.provinceName) return ''

    // 拼接 省，市，区，详细地址 的字符串并返回给用户
    return state.address.provinceName + state.address.cityName + state.address.countyName + state.address.detailInfo
  }
}

export default {
  namespaced: true,
  actions,
  mutations,
  getters,
  state
}
